package bottle.logic;

import java.util.ArrayList;

import org.json.JSONObject;

import bottle.jack.Location;
import bottle.jack.Message;
import bottle.jack.TextContent;

/**
 * The logic behind the bottle server.
 * @author Angus J. Goldsmith
 *
 */
public class BottleLogic {
  /**
   * Data access object for MongoDB.
   */
  private bottle.jack.MongoDAO mongoDao;
  /**
   * The current message id.
   */
  private int currentMessageId;

  /**
   * Constructor, initialises fields.
   */
  public BottleLogic() {
    mongoDao = bottle.jack.MongoDAO.getInstance();
  }
  /**
   * Deals with a DeviceId request.
   * @param deviceId the unique id of the device.
   * @return A JSON object containing the message data or the fact that the
   * device is not currently in the database.
   */
  public final JSONObject dealWithDeviceId(final String deviceId) {
    JSONObject responseJson = new JSONObject();
    boolean deviceExists = mongoDao.checkDeviceExists(deviceId);
    if (deviceExists) {
      responseJson.put("users", mongoDao.getUserList());
      try {
        responseJson.put("messages", mongoDao.getMessagesJson(deviceId));
      } catch (NullPointerException e) {
        responseJson.put("error", e.getLocalizedMessage());
      }
    } else {
      responseJson.put("!exists", "");
    }
    return responseJson;
  }

  /**
   * Deals with the name request.
   * @param deviceId the device id.
   * @param name the name to associate with the device.
   * @return the success or otherwise of the name assign.
   */
  public final JSONObject dealWithName(final String deviceId,
      final String name) {
    JSONObject responseJson = new JSONObject();
    if (mongoDao.addNewDevice(deviceId, name)) {
      responseJson.put("success", "yay");
    } else {
      responseJson.put("error", "this name still exists wat");
    }
    return responseJson;
  }
  /**
   * Deals with the position request.
   * @param deviceId the id of the user's device.
   * @param location the location of the user.
   * @return the success or otherwise.
   */
  public final JSONObject dealWithPosition(final String deviceId,
      final Location location) {
    JSONObject responseJson = new JSONObject();
    if (mongoDao.updateUserLocation(deviceId, location)) {
      responseJson.put("success", "yay");
    } else {
      responseJson.put("error", "this should never happen");
    }
    return responseJson;
  }
  /**
   * Deals with new messages.
   * @param deviceId the sender.
   * @param destinationId the eventual recipient.
   * @param mLocation the location the message is sending from.
   * @param messageContent the content of the message.
   * @return the success of the new message.
   */
  public final JSONObject dealWithNewMessage(final String deviceId,
      final String destinationId,
      final Location mLocation, final String messageContent) {
    JSONObject responseJson = new JSONObject();
    Message message = new Message();
    message.id = Integer.toString(++currentMessageId);
    message.destination  = destinationId;
    final int iterations = 5;
    message.iterationsLeft = iterations;
    TextContent text = new TextContent();
    text.text = messageContent;
    message.messageData = text;
    message.startLocation = mLocation;
    message.rootSender = deviceId;
    message.currentMessageHolder = deviceId;
    message.annotationText = new ArrayList<String>();
    int numMessages = mongoDao.sendNewMessage(deviceId, message);
    responseJson.put("success", numMessages);
    return responseJson;
  }

  /**
   * Deals with add requests.
   * @param deviceId the device in question.
   * @param messageId the message to add to.
   * @param messageData the data to add.
   * @return successful or not.
   */
  public final JSONObject dealWithAdd(final String deviceId,
      final String messageId, final String messageData) {
    JSONObject responseJson = new JSONObject();
    if (mongoDao.addNewAnnotation(deviceId, messageId, messageData)) {
      responseJson.put("success", "yay");
    } else {
      responseJson.put("error", "this should never happen");
    }
    return responseJson;
  }
  /**
   * Calculates the message id.
   */
  public final void calcMessageId() {
    currentMessageId = mongoDao.calcMessageId();
  }

}
